/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;


import Entidades.SFTPFileCopy1;
import Utilidades.Constantes;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author usuario
 */
@WebServlet(name = "DescargarArchivo", urlPatterns = {"/DescargarArchivo"})
public class DescargarArchivo extends HttpServlet {

    SFTPFileCopy1 negFTP;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        response.setContentType("text/html;charset=UTF-8");


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        processRequest(request, response);
        
        negFTP = new SFTPFileCopy1();
        FileOutputStream outPutStream;
        byte[] bytes;
        File file;
        InputStream inputStream;
        int regresar = 0;
        
        if(request.getParameter("ruta") != null){
           try{
                String ruta = negFTP.retornarRutaDownload(request.getParameter("ruta").trim());
                file = new File(ruta);               
               
               try{
                   
                    regresar = Integer.parseInt(request.getParameter("regresar"));
                    inputStream = new FileInputStream(file);      
                    bytes = IOUtils.toByteArray(inputStream);
                    response.setHeader("Content-disposition","attachment; filename="+ruta.replaceAll(" ", "_"));
                    response.getOutputStream().write(bytes);
                    response.getOutputStream().close();
              
               }catch(Exception e){
                    e.getStackTrace();
                    
                    request.setAttribute("descarga", 0);
                   try {
                       request.getRequestDispatcher(Constantes.DIR_VERMAS).forward(request, response);
                   } catch (IOException ex) {
                       Logger.getLogger(DescargarArchivo.class.getName()).log(Level.SEVERE, null, ex);
                   }
               }finally{
                   
                       file.delete();
                       request.setAttribute("regresar", regresar);
                   }
                
            }catch(FileNotFoundException ex){
                Logger.getLogger(DescargarArchivo.class.getName()).log(Level.SEVERE, null, ex);
           } catch (IOException ex) {
                Logger.getLogger(DescargarArchivo.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
        
        
        
        
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
